package com.shuzijun.leetcode.editor.cn;

import java.util.Arrays;

public class SmallestDifferenceLcci{
    public static void main(String[] args) {
         Solution solution = new SmallestDifferenceLcci().new Solution();
         solution.smallestDifference(new int[]{0},new int[]{2147483647});
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {

        public int smallestDifference(int[] a, int[] b) {
            Arrays.sort(a);
            Arrays.sort(b);
            int n = a.length;
            int m = b.length;
            long minRet = Long.MAX_VALUE;
            int i = 0;
            int j = 0;
            while (i<n && j<m){
                if (a[i] >= b[j]){
                    minRet = Math.min(minRet, (long)a[i] - b[j]);
                    j++;
                }else {
                    minRet = Math.min(minRet,   (long)b[j]-a[i]);
                    i++;
                }
            }
            return (int) minRet;
        }
//    public int smallestDifference(int[] a, int[] b) {
//        Arrays.sort(a);
//        Arrays.sort(b);
//        int n = a.length;
//        int m = b.length;
//        long minRet = Long.MAX_VALUE;
//        int i = 0;
//        int j = 0;
//        while (i <n && j < m){
//            if (a[i] >= b[j]){
//                minRet = Math.min(minRet,a[i]-b[j]);
//                j++;
//            }else {
//                minRet = Math.min(minRet,b[j]-a[i]);
//                i++;
//            }
//        }
//        return (int) minRet;
//    }
}
//leetcode submit region end(Prohibit modification and deletion)

}